package tweeks.management.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tweeks.category.domain.Category;
import tweeks.user.domain.Group;
import tweeks.user.domain.Permission;
import tweeks.user.domain.User;
import tweeks.user.service.UserService;

@Controller
@RequestMapping(value="/management/user")
public class UserManagementController {
	
	@Resource(name="UserNormalService")
	private UserService userService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String view(User user, Group group, Permission permission, ModelMap model) throws IOException {
		List<UserDetails> userList = userService.selectUser(user);
		List<Group> groupList = userService.selectGroup(group);
		List<Permission> permissionList = userService.selectPermission(permission);
		model.addAttribute("userList", userList);
		model.addAttribute("groupList", groupList);
		model.addAttribute("permissionList", permissionList);
		return "management/user";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/create")
	public String create(User user, ModelMap model) throws IOException {
		userService.createUser(user);
		return "redirect:/management/user";
	}

	@RequestMapping(method=RequestMethod.POST, value="/update")
	public String update(User user, ModelMap model) throws IOException {
		userService.updateUser(user);
		return "redirect:/management/user";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/delete")
	public String delete(Category user, ModelMap model) throws IOException {
		userService.deleteUser(user.getId());
		return "redirect:/management/user";
	}
}
